var myNumber;
var arr = [];
var code = ['朱彦榜','张洋','于华威','尹良运','杨雪','徐晴','王子昊','王珍','王玉峰','王阳','王威','王思元','司雯','曲洋','欧阳XT','茅佳荣','刘周','刘志坤','刘艺','刘威','刘明杭','接浩','黄锡华','过光锋','顾大为','耿磊','葛勇珍','高君林','董少洋','程仕杰','陈柏君'];

/*随机所有的code并且不重复*/
function showRandomNum(num) {
    var li = "";
    for(var i = 0; i < code.length; i++){
        arr[i] = i;
    }
    arr.sort(function(){
        return 0.5 - Math.random();
    });

    for(var i = 0; i < num; i++){
        var index = arr[i];
        li += '<li>'+code[index]+'</li>';
    }

    $(".prize_list ul").html(li);
}

$(function () {
    const start = document.getElementById("startMusic");
    const end = document.getElementById("endMusic");

    $(".start").click(function(){

        if($("#prize_btn").val() == 0){
            if($("#set_grade").val() == "选择奖等") {
                alert("请选择奖等");
                return;
            }else if($("#prizeCount").val() == "") {
                alert("请输入中奖人数");
                return;
            }else if($("#prizeCount").val() > 10) {
                alert("单次抽奖人数不能超过10人");
                return;
            }else if($("#prizeMoney").val() == "") {
                alert("请输入中奖金额");
                return;
            }else{
                $("#prize_btn").val(1);
                var num = $("#prizeCount").val();
                $(this).find("img").attr("src","images/prize_stop.png");

                myNumber = setInterval(function(){
                    showRandomNum(num);
                }, 30);
            }
            start.play();
            end.pause();
        }else{
            $("#prize_btn").val(0);
            clearInterval(myNumber);
            $(this).find("img").attr("src","images/prize_start.png");
            end.play();
            start.pause();
        }
    });

    //回车键控制开始和停止
    $(document).keydown(function (event) {
        var e = event || window.event || arguments.callee.caller.arguments[0];
        if (e && e.keyCode == 13) { // enter 键
            $(".start").click();
        }
    });

    $("#set_grade").change(function(){
        $(".prize_grade span").text($(this).val());
    });

    $("#prizeMoney").keyup(function(){
        $(".prize_grade i").text($(this).val());
    });
});